#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "i2cbusses.h"
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <unistd.h>
#include <string.h>

/*
	写 ap3216 数据
	1.先写一个寄存器地址，告诉i2c设备我要写入哪个寄存器
	2.再写数据
	参数：
		i2c文件描述符
		设备地址
		寄存器地址
		写入的数据
		数据字节长度
*/
int ap3216_write(int file, unsigned char slave_addr, unsigned char reg, unsigned char *data, int len)
{
	struct i2c_rdwr_ioctl_data rdwr;
	struct i2c_msg i2cmsg;
	unsigned char buf[20];
	int nmsgs_sent;

	buf[0] = reg;
	memcpy(&buf[1], data, len);

	//向寄存器写数据
	i2cmsg.addr = slave_addr;   //i2c设备地址
	i2cmsg.buf = buf;    		//寄存器地址
	i2cmsg.flags = 0;     		//写寄存器操作
	i2cmsg.len = len + 1;       //数据长度

	rdwr.msgs = &i2cmsg;
	rdwr.nmsgs = 1;

	/* 向i2c设备传输数据 */
	nmsgs_sent = ioctl(file, I2C_RDWR, &rdwr);
	
//	if (nmsgs_sent < 0) {
//		printf("Error: Sending messages failed\n");
//		return 1;
//	} else if (nmsgs_sent < rdwr.nmsgs) {
//		printf("Warning: only %d / %d messages were sent\n", nmsgs_sent, rdwr.nmsgs);
//		return 1;
//	}
//	printf("msg sent success\n");
	return 0;
}

/*
	读取 ap3216 数据
	1.先写一个寄存器地址，告诉i2c设备我读哪个寄存器
	2.再读数据
	参数：
		i2c文件描述符
		设备地址
		寄存器地址
		读数据的buff
		读几个字节
*/
int ap3216_read(int file, unsigned char slave_addr, unsigned char reg, unsigned char *data, int len)
{
	struct i2c_rdwr_ioctl_data rdwr;
	struct i2c_msg i2cmsg[2];
	int nmsgs_sent;

	//向寄存器写数据
	i2cmsg[0].addr = slave_addr;    //i2c设备地址
	i2cmsg[0].buf = &reg;    		//寄存器地址
	i2cmsg[0].flags = 0;     		//写寄存器操作
	i2cmsg[0].len = 1;       		//数据长度

	i2cmsg[1].addr = slave_addr;    //i2c设备地址
	i2cmsg[1].buf = data;   		//保存读取数据的缓冲区
	i2cmsg[1].flags = 1;     		//读寄存器操作
	i2cmsg[1].len = len;       		//数据长度

	rdwr.msgs = i2cmsg; 
	rdwr.nmsgs = 2;

	/* 向i2c设备传输数据 */
	nmsgs_sent = ioctl(file, I2C_RDWR, &rdwr);
	
//	if (nmsgs_sent < 0) {
//		printf("Error: Sending messages failed\n");
//		return 1;
//	} else if (nmsgs_sent < rdwr.nmsgs) {
//		printf("Warning: only %d / %d messages were sent\n", nmsgs_sent, rdwr.nmsgs);
//		return 1;
//	}
//	printf("msg sent success\n");
	return 0;
}


int main(int argc, char *argv[])
{
	int i2cbus = 0, file, nmsgs_sent;
	char filename[20];
	unsigned char regs[10] = {0,0xc};
	unsigned char write_data[10] = {0x4, 0x3};
	char read_data[2];
	char slave_addr = 0x1e, read_addr = 0x0c;
	
	int err;

	/* 1.打开i2c设备 */
	file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0);
	if(file < 0){
		printf("open i2c device failed\n");
		return -1;
	}
	printf("open i2c device success! filename = %s\n", filename);

	/* 2.设置i2c设备地址 */
	err = set_slave_addr(file, slave_addr, 1);
	if(err){
		printf("set_slave_addr failed\n");
		return 0;
	}

	/* 3. 发送复位数据0x04到寄存器0地址 */
	err = ap3216_write(file, slave_addr, regs[0], &write_data[0], 1);
	if(err != 0){
		printf("send 0x4 to 0x0 failed!\n");
		return 0;
	}

	/* 4. 发送使能数据0x03到寄存器0地址 */
	err = ap3216_write(file, slave_addr, regs[0], &write_data[1], 1);
	if(err != 0){
		printf("send 0x3 to 0x0 failed!\n");
		return 0;
	}

	while(1)
	{
		/* 5. 读寄存器地址0xc、0xd上的2字节光强数据 */
		err = ap3216_read(file, slave_addr, regs[1], read_data, 2);
		if(err == 0){
//			printf("light data = 0x%02x%02x\n", read_data[1], read_data[0]);
			printf("light data = %d\n", read_data[1] << 8 | read_data[0]);
		}else{
			printf("read light failed!\n");
			return 0;
		}
		sleep(2);
	}
	
	return 0;
}

